/*socket server Handler
Created on 2020年05月10日
@author: zxyong 13738196011
 */

package com.zxy.tcp;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.util.Enumeration;

import com.zxy.business.Analyse_Data;
import com.zxy.common.Com_Fun;
import com.zxy.common.Com_Para;

public class ServerHandler implements Runnable
{
	private BufferedReader	reader;
	private Socket			mServSocket;

	public ServerHandler(Socket clientSocket) throws Exception
	{
		// 得到socket连接
		mServSocket = clientSocket;
	}

	private void init() throws Exception
	{
		try
		{
			if(mServSocket.isConnected())
			{
				Com_Para.htServSockets.put("servSocket" + mServSocket.getInetAddress().getHostAddress(),mServSocket);
				System.out.println("客户端子设备连接：" + mServSocket.getInetAddress().getHostAddress() + " " + Com_Fun.DateTimeStr("yyyy-MM-dd HH:mm:ss:SSS"));
			}
			while(mServSocket != null && mServSocket.isConnected() && !mServSocket.isClosed())
			{
				InputStreamReader isReader = new InputStreamReader(mServSocket.getInputStream(),Com_Para.U_CODE);
				reader = new BufferedReader(isReader);
				mServSocket.setReceiveBufferSize(1024 * 50);
				// 设置缓冲区大小
				char[] Bytes = new char[mServSocket.getReceiveBufferSize()];
				int iLength = reader.read(Bytes,0,Bytes.length);
				if(iLength > 0)
				{
					String strIP = "0.0.0.0";
					/** 解析收到数据 **/
					Analyse_Data ad = new Analyse_Data();
					ad.SubAnalyseRecBytes(iLength,Bytes,mServSocket,true,strIP);
					try
					{
						Thread.sleep(10);
					}
					catch(InterruptedException e)
					{
						e.printStackTrace();
					}
				}
				else if(iLength <= 0)
				{
					System.out.println("客户端链接已关闭链接" + mServSocket.getInetAddress().getHostAddress());
					Com_Para.htServSockets.remove("servSocket" + mServSocket.getInetAddress().getHostAddress());
					String TemChildNode = "";
					for(Enumeration<String> sm = Com_Para.htChildNode.keys();sm.hasMoreElements();)
					{
						TemChildNode = sm.nextElement();
						if(Com_Para.htChildNode.get(TemChildNode) != null && Com_Para.htChildNode.get(TemChildNode).toString().equals("servSocket" + mServSocket.getInetAddress().getHostAddress()))
						{
							break;
						}
					}
					if(TemChildNode != "")
					{
						// Com_Para.htChildInfo.remove(TemChildNode);
						// Com_Para.htChildNode.remove(TemChildNode);
					}
					mServSocket = null;
				}
			}
		}
		catch(IOException e)
		{
			System.out.println("客户端链接已关闭链接" + mServSocket.getInetAddress().getHostAddress());
			Com_Para.htServSockets.remove("servSocket" + mServSocket.getInetAddress().getHostAddress());
			String TemChildNode = "";
			for(Enumeration<String> sm = Com_Para.htChildNode.keys();sm.hasMoreElements();)
			{
				TemChildNode = sm.nextElement();
				if(Com_Para.htChildNode.get(TemChildNode) != null && Com_Para.htChildNode.get(TemChildNode).toString().equals("servSocket" + mServSocket.getInetAddress().getHostAddress()))
				{
					break;
				}
			}
			if(TemChildNode != "")
			{
				// Com_Para.htChildInfo.remove(TemChildNode);
				// Com_Para.htChildNode.remove(TemChildNode);
			}
			mServSocket = null;
		}
	}

	public void run()
	{
		try
		{
			init();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
}